package org.darkforest.controller;

import java.util.Date;
import java.util.List;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.darkforest.auth.AuthPassport;
import org.darkforest.auth.CurrentMember;
import org.darkforest.common.services.ActivitySignService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.constant.Constant;
import org.darkforest.dao.mapper.ActivityGiftMapper;
import org.darkforest.dao.mapper.ActivityMapper;
import org.darkforest.dao.mapper.ActivitySignMapper;
import org.darkforest.dao.mapper.MemberMapper;
import org.darkforest.model.Activity;
import org.darkforest.model.ActivityGift;
import org.darkforest.model.ActivitySign;
import org.darkforest.model.Address;
import org.darkforest.model.Member;
import org.darkforest.model.MemberWithBLOBs;
import org.darkforest.model.RestEntity;
import org.darkforest.vo.ActivitySignVo;
import org.darkforest.vo.AjaxReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "activitySignIn")
@Api(value = "activitySignIn", description = "签到管理",produces= MediaType.APPLICATION_JSON_VALUE)
public class ActivitySignController {

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private ActivitySignMapper activitySignMapper;

    @Autowired
    private ActivityGiftMapper activityGiftMapper;

    @Autowired
    private ActivitySignService activitySignService;

    @Autowired
    private MemberMapper memberMapper;

    @RequestMapping(value = "activityInfo",method = RequestMethod.GET)
    @ApiOperation(value = "活动说明的取得", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity activityInfo() {
        try{
            Activity activity = new Activity();
            activity.setActivityType(Constant.ACTIVITY_TYPE_SIGN);
            activity.setCurrentTime(DateUtil.toUnixTimeStamp(DateUtil.toTimeString(new Date())));
            activity.setActivityState(true);
            List<Activity>  activityList = activityMapper.selectListByCondition(activity);
            return RestEntity.success(activityList);
        }catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed();
        }
    }

    @AuthPassport
    @RequestMapping(value = "signInList",method = RequestMethod.GET)
    @ApiOperation(value = "签到信息的取得", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity signInList(@CurrentMember Member member) {
        try{

            // 是否需要按月份检索??????????
            ActivitySign signsRecord = new ActivitySign();
            signsRecord.setMemberId(member.getMemberId());
            signsRecord.setPageSwitch(false);
            // 礼品状态 0:未到领取时间 1:未领取 2:已领取 (页面处理时的判断条件)
            //  signsRecord.setGiftState("1");
            List<ActivitySign> activitySignList = activitySignMapper.selectListByCondition(signsRecord);
            return RestEntity.success(activitySignList);
        }catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed();
        }
    }

    @RequestMapping(value = "leaderboardList",method = RequestMethod.GET)
    @ApiOperation(value = "排行榜列表", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity leaderboardList(@RequestParam(required = false, defaultValue = "1") Integer  activityId,
                                      @RequestParam(required = false, defaultValue = "0") Integer memberId) {
        try{
            ActivitySign signsRecord = new ActivitySign();
            signsRecord.setActivityId(activityId);
            // 需要分页处理时的条件设定
            //signsRecord.setStart(p);
            //signsRecord.setPageSize(pageSize);
            signsRecord.setPageSwitch(false);

            // 前10条数据的取得
            List<ActivitySign> topContinuousList = activitySignMapper.selectTopContinuousList(signsRecord);
            List<ActivitySign> topTotalList = activitySignMapper.selectTopTotalList(signsRecord);

            // 用户手机号的取得
            for (ActivitySign activitySign: topContinuousList) {
                MemberWithBLOBs memberInfo = memberMapper.selectByPrimaryKey(activitySign.getMemberId());
                // 显示手机号，隐藏中间四位
                String memberMobile = memberInfo.getMemberMobile();
                if (memberMobile != null && memberMobile.length() == 11) {
                    memberMobile = memberMobile.substring(0, 3) + "****" + memberMobile.substring(7, 11);
                }
                activitySign.setMemberMobile(memberMobile);
            }
            for (ActivitySign activitySign: topTotalList) {
                MemberWithBLOBs memberInfo = memberMapper.selectByPrimaryKey(activitySign.getMemberId());
                // 显示手机号，隐藏中间四位
                String memberMobile = memberInfo.getMemberMobile();
                if (memberMobile != null && memberMobile.length() == 11) {
                    memberMobile = memberMobile.substring(0, 3) + "****" + memberMobile.substring(7, 11);
                }
                activitySign.setMemberMobile(memberMobile);
            }

            ActivitySignVo activitySignVo = new ActivitySignVo();
            activitySignVo.setTopContinuousList(topContinuousList);
            activitySignVo.setTopTotalList(topTotalList);

            // 当前登录者的排名的取得
            if (memberId > 0) {
                signsRecord.setMemberId(memberId);
                List<ActivitySign> oneContinuousList = activitySignMapper.selectTopContinuousList(signsRecord);
                List<ActivitySign> oneTotalList = activitySignMapper.selectTopTotalList(signsRecord);

                if (oneContinuousList != null && oneContinuousList.size() > 0) {
                    ActivitySign activitySign = oneContinuousList.get(0);
                    activitySignVo.setOneContinuousSign(activitySign);
                }

                if (oneTotalList != null && oneTotalList.size() > 0) {
                    ActivitySign activitySign = oneTotalList.get(0);
                    activitySignVo.setOneTotalSign(activitySign);
                }
            }

            return RestEntity.success(activitySignVo);
        }catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed();
        }
    }

    @AuthPassport
    @RequestMapping(value = "addSign", method = RequestMethod.POST)
    @ApiOperation(value = "新增签到", httpMethod = "POST",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity addSign(@CurrentMember Member member, @RequestBody ActivitySign signsRecord) {
        try{
            AjaxReturnData ajaxReturnData = activitySignService.insertSelective(member, signsRecord);
            // 签到成功
            if (ajaxReturnData.isResult()) {
                return RestEntity.success(ajaxReturnData);
            } else {
                // 签到时数据有错误时
                // 重复签到
                if ("3".equals(ajaxReturnData.getType())) {
                    return RestEntity.failed(ajaxReturnData.getMessage(), "0");
                } else {
                    return RestEntity.failed(ajaxReturnData.getMessage());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed();
        }
    }
}
